package cn.lingyangwl.agile.system.handler.config;

import cn.lingyangwl.agile.model.enums.ConfigDefineEnum;
import cn.lingyangwl.agile.model.enums.ConfigKeyEnum;
import cn.lingyangwl.agile.model.module.config.param.BaseConfigParam;
import cn.lingyangwl.agile.model.module.config.param.WebSiteParam;
import cn.lingyangwl.agile.model.module.tenant.TenantContext;
import cn.lingyangwl.agile.system.model.entity.SysConfig;
import cn.lingyangwl.framework.tool.core.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author shenguangyang
 */
@Slf4j
public abstract class SmsConfigHandler extends ConfigHandler {

    @Override
    protected void refreshConfig(SysConfig newConfig) {
        // 判断是否使能, 如果使能就刷新, 否则删除
        SysConfig config = configManager.getByConfigKey(ConfigKeyEnum.WEB_SITE.getKey());
        WebSiteParam settingParam = BaseConfigParam.jsonToObject(config.getConfigKey(), config.getConfigValue());

        if (configKey().getKey().equals(settingParam.getEnabledSmsConfigKey())) {
            configEventService.publishRefreshConfigEvent(configAssembly.toConfigMsg(newConfig));
        }
    }

    @Override
    public void doneWriteOps(SysConfig newConfig, BaseConfigParam<?> param) {

    }

    @Override
    public SysConfig doGetUseConfig(ConfigDefineEnum configDefine) {
        SysConfig config = configManager.getByConfigKey(ConfigKeyEnum.WEB_SITE.getKey());
        WebSiteParam settingParam = BaseConfigParam.jsonToObject(config.getConfigKey(), config.getConfigValue());
        if (StringUtils.isEmpty(settingParam.getEnabledSmsConfigKey())) {
            log.info("tenantId: {} 尚未使能短信配置", TenantContext.get().getTenantId());
            return null;
        }
        return configManager.getByConfigKey(settingParam.getEnabledSmsConfigKey());
    }

    @Component
    public static class AliyunSmsConfigHandler extends SmsConfigHandler {

        @Override
        public ConfigKeyEnum configKey() {
            return ConfigKeyEnum.SMS_ALIYUN;
        }
    }

    @Component
    public static class JueMeiSmsConfigHandler extends SmsConfigHandler {
        @Override
        public ConfigKeyEnum configKey() {
            return ConfigKeyEnum.SMS_JUMEI;
        }
    }

    @Component
    public static class TencentSmsConfigHandler extends SmsConfigHandler {
        @Override
        public ConfigKeyEnum configKey() {
            return ConfigKeyEnum.SMS_TENCENT;
        }
    }
}